﻿using MallGo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MallGo.Controllers
{
    public class HomeController : Controller
    {
        //UserContext db = new UserContext();
        ////
        //// GET: /Home/

        public ActionResult Index()
        {
            return RedirectToAction("Register");
        }

        private UserContext db = new UserContext();

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel RegisterModel)
        {
            IEnumerable<User> u = db.User.Where(user => user.UName == RegisterModel.UserName);
            if (u.Count() > 0)
            {
                return RedirectToAction("Index");
            }
            else
            {
                User user = new User();
                user.UName = RegisterModel.UserName;
                user.UPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(RegisterModel.Password, "SHA1");
                db.User.Add(user);
                db.SaveChanges();
                return RedirectToAction("List");
            }
        }


        public ActionResult changePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult changePassword(changePasswordModel changePasswordModel)
        {
            changePasswordModel.OldPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(changePasswordModel.OldPassword, "SHA1");
            changePasswordModel.NewPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(changePasswordModel.NewPassword, "SHA1");
            IEnumerable<User> u = db.User.Where(user => user.UName == changePasswordModel.UserName 
                && user.UPassword == changePasswordModel.OldPassword);
            if (u.Count() == 0)
            {
                return RedirectToAction("Index");
            }
            else
            {
                u.First().UPassword = changePasswordModel.NewPassword;
                db.SaveChanges();
                return RedirectToAction("List");
            }
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel LoginModel)
        {
            LoginModel.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(LoginModel.Password, "SHA1");
            IEnumerable<User> u = db.User.Where(user => user.UName == LoginModel.UserName && user.UPassword == LoginModel.Password);
            if (u.Count() == 0)
            {
                Response.Write("Wrong password!");
                return View();
            }
            else return RedirectToAction("List");
        }

        public ActionResult List()
        {
            List<User> users = db.User.ToList();
            return View(users);
        }
    }
}
